package com.jml.zlhq.app.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jml.zlhq.app.R;
import com.jml.zlhq.app.adapter.ConditionAdapter;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.util.UIUtil;
import com.loopj.android.http.TextHttpResponseHandler;

public class ConditionActivity extends BaseActivity {
	private List<JSONObject> listItems = new ArrayList<JSONObject>();
	private PullToRefreshListView mPullToRefreshView;
	private ListView listView;
	private BaseAdapter adapter;
	private String url;
	private Map<String,String> condition;
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				adapter.notifyDataSetChanged();
				break;
			default:
				break;
			}
		}
	};
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.condition_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.guanbi_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.INVISIBLE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("筛选");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.INVISIBLE);
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		
		Bundle extras = getIntent().getExtras();
		url = extras!=null?extras.getString("url"):"";
		condition = (Map<String, String>) (extras!=null?extras.getSerializable("condition"):new HashMap<String, String>());
	
		mPullToRefreshView = (PullToRefreshListView) findViewById(R.id.condition_listview);
		mPullToRefreshView.setMode(Mode.DISABLED);
		mPullToRefreshView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if(PullToRefreshBase.Mode.PULL_FROM_START == mPullToRefreshView.getCurrentMode()){
					String label = DateUtils.formatDateTime(appContext,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  
					mPullToRefreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
					
				}else if(PullToRefreshBase.Mode.PULL_FROM_END == mPullToRefreshView.getCurrentMode()){
					
				}
			}
		});
		listView = mPullToRefreshView.getRefreshableView();
		//listView.setDividerHeight(UIUtil.dip2px(appContext, 10));
		adapter = new ConditionAdapter(activity, listItems, condition);
		listView.setAdapter(adapter);
		loadData(null);
	}

	@Override
	public void loadData(Map<String, String> param) {
		HttpService.getByText(url, null, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, String resultStr) {
				try {
					JSONObject result = new JSONObject(resultStr);
					JSONArray data = result.getJSONArray("data");
					if(result.getInt("status")==0){
						for (int i = 0; data!=null && i < data.length(); i++) {
							listItems.add(data.getJSONObject(i));
						}
						mHandler.sendEmptyMessage(1);
					}else{
						
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				UIUtil.ToastMessage(activity, "抱歉，访问失败");
				
			}
		});
	
	}

	@Override
	public void clearData() {
		listItems.clear();

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		default:
			break;
		}
	}

}
